


import { defineStore } from 'pinia'

import {reqDance,  reqRecommendVideoList, reqChartsSelect, reqOriginal, reqCourse, reqSpecialTopic, reqInformation, reqAttention ,reqHobbies,reqSearchHistory,reqSearchHobby} from '../api/recommend/recommend'


let useRecommendStore = defineStore('recommendStore', {
    state: () => ({
        // {column,pageSize,pageNum,localCache}
        recommend: {
            videoList: [],
            column: "recommend",
            pageSize: 20,
            pageNum: 0,
            localCache: 0
        },
        // 作品
        original: {
            videoList: [],
            column: "original",
            pageSize: 20,
            pageNum: 0,
            id:"integrated "
        },
        // //integrated   //popTraining //newcomer  //lastest

        //教程
        course: {
            videoList: [],
            column: "course",
            pageSize: 20,
            pageNum: 0,
            id:"integrated "
        },
        // 专题
        specialTopic: {
            videoList: [],
            column: "specialTopic",
            pageSize: 20,
            pageNum: 0,
            id:"integrated "
        },
        // 动态哦
        information: {
            videoList: [],
            column: "frontDynamic",
            pageSize: 20,
        },
        // 关注
        attention: {
            videoList: [],
            pageNum: 0,
            pageSize: 8,
            column: "attention",
        },
        //各种舞蹈分类
        dance: {
            videoList: [],
            column: "",
            pageSize: 20,
            pageNum: 0,
            localCache: 0
        },
        charts: [],


        // 兴趣爱好列表
        hobby: [],
        // 兴趣爱好搜索历史
        searchHistory:[],
        // 搜索兴趣技能
        searchHobbies:[],
        hotId:"",
        hotName:"integrated ",
        loading:false,
        // 选中的兴趣技能
        checkedHobbies:JSON.parse(localStorage.getItem("hobbyList"))||[]
        
        


    }),

    actions: {
        // 获取推荐列表
        async getRecommendVideoList() {
            this.loading = true;
            let { column, pageNum, pageSize, localCache } = this.recommend;
            try {
                let result = await reqRecommendVideoList({ column, pageNum, pageSize, localCache });

                this.recommend.videoList.push(...result.content);
                console.log("此处为store仓库");
                this.recommend.pageNum++;
                this.recommend.localCache += this.recommend.pageSize;
                this.loading = false;
            } catch (error) {

            }

        },
        // 获取作品列表
        async getOriginalVideoList() {
            this.loading = true;
            let { column, pageNum, pageSize} = this.original;
            let data = this.reqData;
            // console.log(data);
            
            try {
                let result = await reqOriginal({id:this.hotName, column, pageNum, pageSize, data });

                this.original.videoList.push(...result.content);
                // console.log(...result.content);
                this.original.pageNum++;
                this.loading = false;
                // this.original.localCache += this.original.pageSize;
            } catch (error) {

            }

        },
           // 获取教程列表
           async getCourseVideoList() {
            this.loading = true;
            let { column, pageNum, pageSize } = this.course;
            let data = this.reqData;
            try {
                let result = await reqCourse({id:this.hotName, column, pageNum, pageSize, data });

                this.course.videoList.push(...result.content);
                // console.log(...result.content);
                this.course.pageNum++;
                this.loading = false;
                // this.original.localCache += this.original.pageSize;
            } catch (error) {

            }

        },
          // 获取专题列表
          async getSpecialTopicVideoList() {
            this.loading = true;
            let { column, pageNum, pageSize } = this.specialTopic;
            let data = this.reqData;
            try {
                let result = await reqSpecialTopic({id:this.hotName, column, pageNum, pageSize, data });

                this.specialTopic.videoList.push(...result.content);
                // console.log(...result.content);
                this.specialTopic.pageNum++;
                this.loading = false;
                // this.original.localCache += this.original.pageSize;
            } catch (error) {

            }

        },
        // 获取动态列表
        async getInformationVideoList() {
            this.loading = true;
            let { column,  pageSize } = this.information;
            try {
                let result = await reqInformation({ column,  pageSize});

                this.information.videoList.push(...result.content);
                this.loading = false;
                // console.log(...result.content);
                // this.information.pageNum++;
                // this.original.localCache += this.original.pageSize;
            } catch (error) {

            }

        },
          // 获取关注列表
          async getAttentionList() {
            this.loading = true;
            let { pageNum,  pageSize } = this.attention;
            try {
                let result = await reqAttention({ pageNum,  pageSize});

                this.attention.videoList=result.content;
                // console.log(...result.content);
                this.attention.pageNum++;
                this.loading = false;
                // this.original.localCache += this.original.pageSize;
            } catch (error) {

            }

        },
        // 获取舞蹈列表
        async getDanceVideoList(column) {
            this.loading = true;
            let {  pageNum, pageSize, localCache} = this.dance;

            try {
                let result = await reqDance({ column, pageNum, pageSize, localCache });

                this.dance.videoList.push(...result.content);
                this.dance.pageNum++;
                this.dance.column = column;
                this.dance.localCache += this.dance.pageSize;
                this.loading = false;
                 if(result.content.length==0){
                   return Promise.reject("未查询到相关数据")
                  }
            } catch (error) {

            }

        },
        async getCharts() {
            let result = await reqChartsSelect();
            this.charts = result;
            // console.log(this.charts);
        },
        // 切换页面时清空仓库数据
        clearData() {
            this.recommend = {
                videoList: [],
                column: "recommend",
                pageSize: 20,
                pageNum: 0,
                localCache: 0
            };
            // 作品
            this.original = {
                videoList: [],
                column: "original",
                pageSize: 20,
                pageNum: 0,
                id:"integrated "
            };
            //教程
            this.course = {
                videoList: [],
                column: "course",
                pageSize: 20,
                pageNum: 0,
                id:"integrated "
            };
            // 专题
            this.specialTopic = {
                videoList: [],
                column: "specialTopic",
                pageSize: 20,
                pageNum: 0,
                id:"integrated "
            };
            // 动态哦
            this.information = {
                videoList: [],
                column: "frontDynamic",
                pageSize: 20,
            };
            // 关注
            this.attention = {
                videoList: [],
                pageNum: 0,
                pageSize: 8,
                column: "attention",
            };
            //各种舞蹈分类
        this.dance = {
            videoList: [],
            column: "",
            pageSize: 20,
            pageNum: 0,
            localCache: 0
        };
            // console.log(this.charts);
        },

        // 兴趣爱好数据
        async getHobbies(pageNum, pageSize) {

            let num = 0   
            while (num <  2) {

                try {
                    let result = await reqHobbies(pageNum, pageSize)
                    this.hobby.push(...result.content)
                    num++
                    pageNum++;
                    

                } catch (error) {

                }
            }
          
        },

        // 兴趣爱好搜索历史
        async getSearchHistory (){
           let result = await reqSearchHistory()
        //    console.log(result,'111');
           this.searchHistory = result
        },
        // 点击兴趣爱好确定按钮
        async getHobbiesSave(pageNum){
            let { column, pageSize, localCache } = this.recommend;
            let result = await reqRecommendVideoList({ column, pageNum, pageSize, localCache })
            console.log(result);
           
        },
        //搜索兴趣爱好
        async getSearchHobby(name){
           let result= await reqSearchHobby(name)
           console.log(result);
           this.searchHobbies = result
        }

    },   
    
    
    getters:{
         // 计算面包屑中保留的数据

         hobbies(state){
           return  state.hobby.map(item=>{
                item.flag = state.checkedHobbies.some(item1=>item1.id==item.id);
                return item;
            })
         },

        avocation(state){           
            return ((state.charts[0]||{}).choises||[]).filter(item=>item.select)
        },
        contentType(state){
            return ((state.charts[1]||{}).choises||[]).filter(item=>item.select)
        },
        // 整合榜单数据
        reqData(){
            
            let data = {
                filterInfo: [
                ]
            };
            let n1,n2;
            if(this.avocation.length){
                n1 = {
                    filterCode: "avocation",
                    avocationNames:[],
                    filterValue:[]
                }
                this.avocation.forEach(item=>{
                    n1.avocationNames.push(item.name);
                    n1.filterValue.push(item.id);
                })  
                data.filterInfo.push(n1) ;           
            }
            if(this.contentType.length){
                 n2 = {
                    filterCode: "contentType",
                    filterValue:[]
                }
                this.contentType.forEach(item=>{
                    n2.filterValue.push(item.id);
                }) 
                data.filterInfo.push(n2) ; 
            }
            console.log("data数据",data);
            
            return data;
        }

    }
})
export default useRecommendStore